#ifndef DBMS_SERVER_GLOBAL_DEFINITIONS_H
#define DBMS_SERVER_GLOBAL_DEFINITIONS_H

// ========================================
// DBMS Server - Global Configuration
// ========================================

#include <string>
#include <Winsock2.h>

namespace DBMS {
    namespace Server {

        // ========================================
        // 基础数据类型定义
        // ========================================
        using int8_t = signed char;
        using uint8_t = unsigned char;
        using uint_t = unsigned int;
        using uint16_t = unsigned short;
        using int16_t = short;
        using uint32_t = unsigned int;
        using int32_t = int;

        // B+树专用类型
        using BPlusTreeKeyType = double;
        using BPlusTreeDataType = std::string;

        // ========================================
        // B+树配置参数
        // ========================================
        namespace BPlusTreeConfig {
            constexpr int TREE_ORDER = 20;                           // B+树的阶数
            constexpr int MIN_KEYS = TREE_ORDER - 1;                // 节点最小键值数量
            constexpr int MAX_KEYS = 2 * TREE_ORDER - 1;            // 节点最大键值数量
            constexpr int MIN_CHILDREN = MIN_KEYS + 1;               // 内部节点最小子节点数
            constexpr int MAX_CHILDREN = MAX_KEYS + 1;               // 内部节点最大子节点数
            constexpr int MIN_LEAF_ENTRIES = MIN_KEYS;               // 叶子节点最小条目数
            constexpr int MAX_LEAF_ENTRIES = MAX_KEYS;               // 叶子节点最大条目数
        }

        // ========================================
        // 存储系统配置
        // ========================================
        namespace StorageConfig {
            constexpr int BLOCK_SIZE_BYTES = 4096;                  // 数据页大小（字节）
            constexpr int MAX_BLOCKS_PER_FILE = 1048576;            // 每文件最大页数
            constexpr int BUFFER_POOL_SIZE = 5;                     // 缓冲池大小
            constexpr float FREE_SPACE_THRESHOLD = 0.3f;            // 空闲空间阈值
            constexpr float USED_SPACE_THRESHOLD = 0.7f;            // 使用空间阈值
        }

        // ========================================
        // 网络通信配置
        // ========================================
        namespace NetworkConfig {
            constexpr int DEFAULT_SERVER_PORT = 8080;               // 默认服务器端口
            constexpr int MAX_CONCURRENT_THREADS = 10;              // 最大并发线程数
            constexpr int INVALID_INDEX_VALUE = -1;                 // 无效索引标识
        }

        // ========================================
        // 枚举类型定义
        // ========================================
        enum class DatabaseBlockType {
            index,
            table,
            dictionary
        };

        enum class ComparisonOperator {
            LESS_THAN,
            LESS_EQUAL,
            EQUAL,
            GREATER_EQUAL,
            GREATER_THAN,
            NOT_EQUAL
        };

        // ========================================
        // 网络连接结构体
        // ========================================
        struct NetworkConnection {
            SOCKET socketHandle;

            NetworkConnection() : socketHandle(INVALID_SOCKET) {}
            explicit NetworkConnection(SOCKET sock) : socketHandle(sock) {}
        };

    } // namespace Server
} // namespace DBMS

using namespace DBMS::Server;

typedef int8_t int8;
typedef uint8_t uint8;
typedef uint_t uint;
typedef uint16_t uint16;
typedef int16_t int16;
typedef uint32_t uint32;
typedef int32_t int32;
typedef BPlusTreeKeyType BPTKeyType;
typedef BPlusTreeDataType BPTDataType;

const int ORDER = BPlusTreeConfig::TREE_ORDER;
const int MINNUM_KEY = BPlusTreeConfig::MIN_KEYS;
const int MAXNUM_KEY = BPlusTreeConfig::MAX_KEYS;
const int MINNUM_CHILD = BPlusTreeConfig::MIN_CHILDREN;
const int MAXNUM_CHILD = BPlusTreeConfig::MAX_CHILDREN;
const int MINNUM_LEAF = BPlusTreeConfig::MIN_LEAF_ENTRIES;
const int MAXNUM_LEAF = BPlusTreeConfig::MAX_LEAF_ENTRIES;

#define BLOCK_SIZE StorageConfig::BLOCK_SIZE_BYTES
#define BLOCK_NUM StorageConfig::MAX_BLOCKS_PER_FILE
#define DATA_BUFFER_BLOCK_NUM StorageConfig::BUFFER_POOL_SIZE
#define PCT_FREE_THRESHOLD StorageConfig::FREE_SPACE_THRESHOLD
#define PCT_USED_THRESHOLD StorageConfig::USED_SPACE_THRESHOLD

const short server_my_port = NetworkConfig::DEFAULT_SERVER_PORT;
const int max_thread_num = NetworkConfig::MAX_CONCURRENT_THREADS;
const int INVALID_INDEX = NetworkConfig::INVALID_INDEX_VALUE;

using BlockType = DatabaseBlockType;
enum COMPARE_OPERATOR { LT, LE, EQ, BE, BT, BETWEEN };

//性能监控配置参数
#define PAGE_SIZE 4096
#define PAGES_PER_FILE 1048576  // 每个文件拥有的页数
#define BUFFER_POOL_CAPACITY 5 //缓冲池页数容量
#define HOT_PAGE_THRESHOLD 3
#define HIGH_THROUGHPUT_THRESHOLD 50
#define LOW_THROUGHPUT_THRESHOLD 30
#define MONITOR_INTERVAL 1
#define PREFETCH_THRESHOLD 60

enum class PageType { /*DATA_PAGE, INDEX_PAGE, DIRECTORY_PAGE, FREE_PAGE*/index, table, dictionary
};

struct F {
    SOCKET sockConn;
    F() : sockConn(INVALID_SOCKET) {}
    explicit F(SOCKET sock) : sockConn(sock) {}
};

#ifndef NULL
#define NULL 0
#endif

#endif // DBMS_SERVER_GLOBAL_DEFINITIONS_H
